package com.lz.app.demo.configuration;

import com.lz.app.demo.util.mapper.BeanMapperTemplate;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;


import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Title: BeanMapperAutoConfiguration
 * @ProjectName app-service
 * @Author huchao
 * @Date 2018/9/5下午2:56
 */
@ConditionalOnClass(ModelMapper.class)
@ConditionalOnProperty(prefix = "modelMapper", value = "enabled", havingValue = "true", matchIfMissing = true)
@Configuration
public class BeanMapperAutoConfiguration {

	private ModelMapper modelMapper(boolean ignoreNull) {
		ModelMapper modelMapper = new ModelMapper();
		modelMapper.getConfiguration()
				// 是否忽略空属性
				.setSkipNullEnabled(ignoreNull)
				.setAmbiguityIgnored(true)
				.setImplicitMappingEnabled(true)
				.setFullTypeMatchingRequired(false)
				.setMatchingStrategy(MatchingStrategies.STRICT)
				.setFieldAccessLevel(org.modelmapper.config.Configuration.AccessLevel.PRIVATE)
				.setMethodAccessLevel(org.modelmapper.config.Configuration.AccessLevel.PRIVATE);
		return modelMapper;
	}

	@ConditionalOnMissingBean(BeanMapperTemplate.class)
	@Bean
	public BeanMapperTemplate beanMapperTemplate() {

		return new BeanMapperTemplate(modelMapper(false), modelMapper(true));
	}
}
